package cn.bone.ou.community.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.Ref;
import cn.bone.ou.cache.RegionCache;
import cn.bone.ou.community.bean.Community;
import cn.bone.ou.community.bean.SysRegion;
import cn.bone.ou.community.mapper.CommunityMapper;
import cn.bone.ou.community.param.CommunityParam;
import cn.bone.ou.community.service.ICommunityService;
import cn.bone.ou.community.vo.CommunityVo;
import cn.bone.ou.myuser.vo.RegionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CommunityServiceImpl implements ICommunityService {

    @Resource
    private CommunityMapper communityMapper;
    @Resource
    private RegionCache regionCache;

    public CommonResult<CommunityVo.GetInfoVo> getInfo(CommunityParam.GetInfoParam param){
        Community community = communityMapper.selectById(param.getCommunityId());
        CommunityVo.GetInfoVo vo = new CommunityVo.GetInfoVo();
        vo.setCommunityId(community.getId());
        vo.setName(community.getName());
        vo.setProvince(community.getProvince());
        vo.setProvinceName(regionCache.get(community.getProvince()).getName());
        vo.setCity(community.getCity());
        vo.setCityName(regionCache.get(community.getCity()).getName());
        vo.setDistrict(community.getDistrict());
        vo.setDistrictName(regionCache.get(community.getDistrict()).getName());
        vo.setAddress(community.getAddress());
        vo.setHouseNum(community.getHouseNum());
        vo.setBuildAreaNum(community.getBuildAreaNum());
        vo.setExclusiveArea(community.getExclusiveArea());
        return Ref.ok(vo);
    }

    @MyLogger(msg = "设置小区信息")
    public CommonResult setInfo(CommunityParam.SetInfoParam param){
        Community community = communityMapper.selectById(param.getCommunityId());
        community.setHouseNum(param.getHouseNum());
        community.setBuildAreaNum(param.getBuildAreaNum());
        community.setExclusiveArea(param.getExclusiveArea());
        String province = param.getRegionNo().substring(0,2)+"0000";
        String city = param.getRegionNo().substring(0,4)+"00";
        community.setProvince(province);
        community.setCity(city);
        community.setDistrict(param.getRegionNo());
        community.setAddress(param.getAddress());
        communityMapper.updateById(community);
        return Ref.ok();
    }

    public CommonResult<List<RegionVo.ListVo>> regionList(){
        List<SysRegion> list = regionCache.list();
        List<RegionVo.ListVo> vos = list.stream().map(region->{
            RegionVo.ListVo vo = new RegionVo.ListVo();
            vo.setName(region.getName());
            vo.setNo(region.getNo());
            vo.setLevel(region.getLevel());
            vo.setPno(region.getPno());
            return vo;
        }).collect(Collectors.toList());
        return Ref.ok(vos);
    }
}
